// pages/home-page/demonstration/demonstration.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startPageY: 0,
    list:[],
    flag:false,
    productId: '',
    customerId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { productId, customerId } = options
    this.setData({
      productId: productId,
      customerId: customerId
    })
    this.addStage()
    this.getWxmlCode('#moveBox',(resp)=>{
      this.setData({moveBox:resp})
      setTimeout(() => {
        this.getWxmlCode('#movelist0',(res)=>{
          this.setData({movelist0:res})
          var space = res.top-resp.top
          var half = res.bottom - res.top + space
          this.setData({
              itemHeight: res.bottom - res.top,
              space: half, //两条中间到另一条的距离
              beLeftSpace: resp.top-(res.bottom - res.top)/2, //位置要减去距离
            })
          })
      },300)
    })
    this.getDemonstrationList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addStage () {
    const { customerId, productId } = this.data
    app.api.post({
      url: 'customer_stage_add',
      data: {
        customerId,
        productId,
        stage: 'show' // 演示阶段
      }
    })
  },
  getDemonstrationList () {
    app.api.post({
      url: 'product_vx_presentation_list',
      data: {productId: this.data.productId}
    }).then(res => {
      let arr = res
      let defaultList = [{title: '利益演示', description: '不同保单年度、年龄等利益介绍'}]
      let newList = arr.concat(defaultList)
      newList.forEach((v) => {
        v.active = false
      })
      newList[newList.length -1].active = true
      console.log(newList)
      this.setData({
        list: newList
      })
    })
  },
  getWxmlCode(str,cal){
    const query = wx.createSelectorQuery()
    query.select(str).boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((res) => {
    if(cal) cal(res[0])
    })
  },
  toggleTrain () {
    const { customerId, productId } = this.data
    wx.navigateTo({
      url: `/pages/home-page/train/train?customerId=${customerId}&productId=${productId}`
    })
  },
  toggle (e) {
    let arr = this.data.list
    arr.forEach((v, k) => {
      if (k === e.currentTarget.dataset.index) {
        if (v.active) {
          v.active = false
        } else {
          v.active = true
        }
      }
    })
    this.setData({
      list: arr
    })
    console.log(e.currentTarget.dataset.index)
  },
  listItemlongtap (e) {
    console.log('长按', e)
    this.setData({
      flag:true
    })
  },
  listItemStart (e) {
    console.log(e)
    this.setData({
      startPageY: e.touches[0].pageY
    })
    console.log(e.touches[0].pageY, this.data.itemHeight)
  },
  listItemMove (e) {
    console.log(e)
    // console.log(e.touches[0].pageY, this.data.itemHeight)
    if(this.data.flag){
      if (e.touches[0].pageY < this.data.itemHeight) {
        return false
      }
      var moveTop = e.touches[0].pageY-this.data.itemHeight
      var moveoutIndex = parseInt((moveTop-this.data.beLeftSpace)/this.data.space)
      if(e.currentTarget.dataset.index<=moveoutIndex) moveoutIndex++
      this.moveoutIndex = moveoutIndex
      this.setData({nowmoveIndex:e.currentTarget.dataset.index,moveTop,moveoutIndex})
    }
  },
  listItemEnd(e){
    if (!this.data.flag) {
      return;
    }
    console.log(e, e.changedTouches[0].pageY)
    if (this.data.startPageY === e.changedTouches[0].pageY) {
      return false
    }
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    let data = {...list[index]}
    list.splice(index,1)
    list.splice(this.moveoutIndex,0,data)
    this.setData({list:list,moveoutIndex:-1,nowmoveIndex:-1, flag: false})
  },
  toDemonstration(){
    let presentationIds = []
    let selectedIds = []
    let presentationIdsSelected = []
    this.data.list.forEach((v) => {
      if (v.presentationId && v.active) {
        presentationIds.push(v.presentationId)
      }
      if (v.active) {
        selectedIds.push(v.active)
      }
      if (!v.presentationId && v.active) {
        presentationIdsSelected.push(v.active)
      }
    })
    if ((presentationIdsSelected.length > 0 && presentationIds.length > 0) || (presentationIdsSelected.length === 0 && presentationIds.length > 0)) {
      if (presentationIdsSelected.length > 0) {
        wx.navigateTo({
          url: `/pages/home-page/to-demonstration/toDemonstration?productId=${this.data.productId}&customerId=${this.data.customerId}&type=demonstration&presentationIds=${presentationIds}&hasSelectInterest=true`
        })
      } else {
        wx.navigateTo({
          url: `/pages/home-page/to-demonstration/toDemonstration?productId=${this.data.productId}&customerId=${this.data.customerId}&type=demonstration&presentationIds=${presentationIds}`
        })
      }
    }
    if (presentationIdsSelected.length > 0 && presentationIds.length === 0) {
      app.api.post({
        url: 'product_vx_presentation_start',
        data: {presentationIds: [],productId: this.data.productId}
      }).then(res => {
        console.log(res)
      })
      wx.navigateTo({
        url: `/pages/home-page/project/project?customerId=${this.data.customerId}&productId=${this.data.productId}&check=1`
      })
    }
    if (selectedIds.length === 0) {
      wx.showToast({
        title: '请选择演示内容',
        icon: 'none',
        duration: 2000
      })
    }
  }
})